<?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use frontend\components\AuthBehavior;
use frontend\components\RulesoBehavior;
use frontend\helpers\Tooles;
use common\models\Signup;
use backend\models\UploadForm;
use yii\web\UploadedFile;

/**
 * Site controller
 */
class SignupController extends Controller
{

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'wechatauths' => [
            'class' => AuthBehavior::className()
            ],
            'voteRules' => [
                'class' => RulesoBehavior::className()
            ],
        ];
    }

    public function actionIndex()
    {

        $model = new Signup();
        $session = Yii::$app->session; 
        $userinfo = Yii::$app->params['userinfo'];
        //var_dump(\Yii::$app->request->post());

        if(Yii::$app->request->isPost){
            $Image = new UploadForm();
            $model->load(Yii::$app->request->post());
            $Image->imageFile = UploadedFile::getInstance($model, 'thumb');
            if(!empty($Image->imageFile) && $Image->upload()){
                $model->thumb = $Image->imageFile;
            }

            //设置参数
            $model->openid = $userinfo['openid'];
            $model->nickname = $userinfo['nickname'];


            if($model->save()){
                Yii::$app->session->setFlash('success', '报名成功');
                return $this->redirect(['index'])->send();
                exit;
            }else{
                var_dump($model->errors);exit;
            }

        }

        return $this->render('index', [
            'model' => $model,
        ]);
    }


}


